'use strict'

$(function(){
	var inputUsername =$('#input_username'),
		inputPassword =$('#input_password'),
		inputConfirmPassword =$('#input_confirmpassword'),
		inputEmail =$('#input_email'),
		commitBtn =$('#regist_button'),
		inputForm = $('#regist_form').children().children().children('input'),
		registForm = $('#regist_form'),
		errflag = [],
		//正在提交状态，若正在提交则有些内容不需判断
		iscommit = false


	//初始化errflag和input标签打标记
	for(let i=0;i<inputForm.length;i++){
		inputForm.eq(i).attr('data-number',i)
		errflag.push(true)
	}


	//用户名选项框相关判断
	inputUsername.on('blur',function(){
		if(!iscommit){
		//用户名是否为空判定
		if($(this).val()==''|| $(this).val() == undefined){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'用户名不能为空')
			$('#regist_button').attr('disabled',true)
			return 
		}

		isOk($(this))

		}

	})

	inputUsername.on('keyup',function(){
		$('#regist_button').attr('disabled',true)
		 clearError($(this))

	})
	//密码选项框相关判断
	inputPassword.on('blur',function(){
		if(!iscommit){
		//密码是否为空判定
		if($(this).val()==''|| $(this).val() == undefined){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'密码不能为空')
			$('#regist_button').attr('disabled',true)
			return 
			
		}
		//密码是否符合规范判定
		var regexp =/^[A-Za-z0-9]+$/
		if($(this).val().length<6 || $(this).val().length>16 || !regexp.test($(this).val())){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'密码应由6-12位之间的数字或字母构成')
			$('#regist_button').attr('disabled',true)
			return
		}

		isOk($(this))

		}

	})

	inputPassword.on('keyup',function(){
		$('#regist_button').attr('disabled',true)
		clearError($(this))
	})

	//确认密码选项框相关判断
	inputConfirmPassword.on('blur',function(){
		if(!iscommit){
		//确认密码为空判断
		if($(this).val()==''|| $(this).val() == undefined){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'确认密码不能为空')
			$('#regist_button').attr('disabled',true)
			return
		}

		//确认密码与密码一致性判断
		if($(this).val() != inputPassword.val()){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'确认密码与密码不一致')
			$('#regist_button').attr('disabled',true)
			return 
		}

		isOk($(this))

	    }
	})

	inputConfirmPassword.on('keyup',function(){
		$('#regist_button').attr('disabled',true)
		clearError($(this))
	})
	//邮箱选项框相关判断
	inputEmail.on('blur',function(){
		if(!iscommit){
		var that =$(this)
		if($(this).val()==''|| $(this).val() == undefined){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'邮箱不能为空')
			$('#regist_button').attr('disabled',true)
			return
			
		}

		var regexp = /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.com$/
		if(!regexp.test($(this).val())){
			errflag[$(this).attr('data-number')] = true
			isNull($(this),'邮箱格式不正确')
			$('#regist_button').attr('disabled',true)
			return
			
		}


		$.ajax({
			type:'post',
			url:'/signup/verifyEmail',
			data:{
				email:$(this).val()
			},

			success:function(data){

				if(data.status == 1 && data.regist == true){
					errflag[$(this).attr('data-number')] = true
					isNull(that,'邮箱已被注册')
					$('#regist_button').attr('disabled',true)

				}else if(data.status == 1 && data.regist == false){

					isOk(that)

				}else{

					//TODO,错误情况暂未处理
				}




			}
		})

		}

	})


	inputEmail.on('keyup',function(){
		$('#regist_button').attr('disabled',true)
		clearError($(this))

	})


	commitBtn.on('click',function(){

		var that =$(this)
		var user ={
			username:inputUsername.val(),
			password:inputPassword.val(),
			email:inputEmail.val()
		}
		$.ajax({
			type:'post',
			url:'/signup/save',
			data:{
				user:user
			},
			success:function(data){
				if(data.status == 1){
					iscommit=true
					that.attr('disabled',true)
		            inputUsername.attr('readonly',true)
					inputPassword.attr('readonly',true)
					inputConfirmPassword.attr('readonly',true)
					inputEmail.attr('readonly',true)
					var timer
					var limitTime =5 
					var regMsg = $('.reg-msg')
					regMsg.text('注册成功，'+limitTime+'秒后跳转到登录页面').show()
					timer = setInterval(function(){
						if(limitTime == 0){
							clearInterval(timer)
								registForm.submit()
							}
					regMsg.text('注册成功，'+limitTime+'秒后跳转到登录页面')
					limitTime--
					},1000)


				}else{

					regMsg.text('注册失败').show()

				}
			}
		})

	})

function isOk(node){
	errflag[node.attr('data-number')] = false
	var isDisabled = true 
	for(let i=0;i<errflag.length;i++){

		 		if(errflag[i] == true){
		 			isDisabled = true
		 			break;
		 		}

		 		isDisabled = false

		 	}
		 if(!isDisabled){
		 		$('#regist_button').removeAttr("disabled")

		 	}

	node.next('.status-icon').addClass("fa-check")
	node.parent().addClass('has-success')
	
}


})








